﻿<div id="gridForms"></div>
<div id="dlgAddForm" style="display:none;padding-left:60px;">
    <form id="formAddForm"></form>
</div>
<div id="dlgUpdateForm" style="display:none;padding-left:60px;">
    <form id="formUpdateForm"></form>
</div>

<script type="text/javascript">
    var gridForms = null;
    var dlgAddForm = null;
    var dlgUpdateForm = null;
    var formAddForm = null;
    var formUpdateForm = null;
</script>

<script type="text/javascript">
    function addForm() {
        $.post('/API/Form/Create', formAddForm.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('添加成功', '提示');
                loadForms();
                dlgAddForm.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function showDlgAddForm() {
        if (dlgAddForm) {
            dlgAddForm.show();
        }
        else {
            dlgAddForm = $.ligerDialog.open({
                target: $('#dlgAddForm'),
                width: 440,
                height: 150,
                title: '添加新表单',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formAddForm.valid()) {

                                addForm();
                            }
                            else {
                                formAddForm.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgAddForm.hide();
                        }
                    }
                ]
            });
        }
    }

    function initAddForm() {
        formAddForm = $('#formAddForm').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { display: '表单名称', name: 'FormName', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });
    }
</script>

<script type="text/javascript">
    function updateForm() {
        $.post('/API/Form/Update', formUpdateForm.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('修改成功', '提示');
                loadForms();
                dlgUpdateForm.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function showDlgUpdateForm(row) {
        if (dlgUpdateForm) {
            dlgUpdateForm.show();
        }
        else {
            dlgUpdateForm = $.ligerDialog.open({
                target: $('#dlgUpdateForm'),
                width: 440,
                height: 150,
                title: '修改表单',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formUpdateForm.valid()) {
                                updateForm();
                            }
                            else {
                                formUpdateForm.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgUpdateForm.hide();
                        }
                    }
                ]
            });
        }

        formUpdateForm.setData(row);
    }

    function initUpdateForm() {
        formUpdateForm = $('#formUpdateForm').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { name: 'Id', type: 'hidden' },
                { display: '表单名称', name: 'FormName', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });
    }

    function deleteForm(row) {
        $.post('/API/Form/Delete', { Id: row.Id, LoginName: row.LoginName },
            function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('删除成功', '提示');
                    loadForms();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
    }
</script>

<script type="text/javascript">
    function itemclick(item) {
        switch (item.method) {
            case 'add':
                showDlgAddForm();
                break;

            case 'update':
                DoAction(gridForms, function (row) {
                    showDlgUpdateForm(row);
                }, '请选择需要修改的表单');
                break;

            case 'delete':
                DoActionConfirm(gridForms, deleteForm, '请选择需要修删除的表单', '是否要删除当前选择的表单？');
                break;
        }
    }

    var toolbarForms = {
        items: [
            { method: 'add', text: '增加', click: itemclick, icon: 'add' },
            { line: true },
            { method: 'update', text: '修改', click: itemclick, icon: 'modify' },
            { line: true },
            { method: 'delete', text: '删除', click: itemclick, icon: 'delete' }
        ]
    };

    var columnForms = [
        { display: '', name: 'Id', align: 'left', hide: true },
        { display: '表单名称', name: 'FormName', align: 'left', width: 150, minwidth: 120 },
        {
            display: '操作', align: 'left', width: 150, minwidth: 120,
            render: function (_row) {
                return '<a href="/WF/Home/Fields?id=' + _row.Id + '" target="_self"">定义字段</a>';
            }
        }
    ];

    function loadForms() {
        var complete = false;
        $.post('/API/Form/Query', {}, function (data) {
            complete = true;
            gridForms.loadData(data);
            gridForms.gridloading.hide(100);
        }, 'json');

        var n = setInterval(function () {
            clearInterval(n);
            if (!complete) {
                gridForms.gridloading.show();
            }
        }, 100);
    }

    $(document).ready(function () {
        $.metadata.setType("attr", "validate");
        initAddForm();
        initUpdateForm();
        gridForms = $('#gridForms').ligerGrid({
            title: '表单信息',
            columns: columnForms,
            toolbar: toolbarForms,
            rownumbers: true,
            resizable: false,
            frozen: false,
            pageSize: 20,
            dataAction: 'local'
        });

        loadForms();
    });
</script>